package com.huluobo.jetpackdemo.paging

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.huluobo.jetpackdemo.R
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class PagingActivity : AppCompatActivity() {
    private val pagingAdapter = PagingAdapter()
    private val vm by lazy { ViewModelProvider(this)[PagingViewModel::class.java] }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_paging)
        val rv = findViewById<RecyclerView>(R.id.paging_rv)
        rv.layoutManager = LinearLayoutManager(this)
        rv.adapter = pagingAdapter

        lifecycleScope.launch {
            vm.getPagingData().collect {
                pagingAdapter.submitData(it)
            }
        }
    }
}